/**
 * Copyright (c) 2025 myons Inc. All rights reserved.
 */
package cn.zmwh.common.security.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.zmwh.common.security.annotation.PreRole;
import cn.zmwh.common.security.pojo.MethodRole;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @describe: 加载权限 、
 * @author: dmzmwh 、
 * @time: 2025-03-17 15:02 、
 */
@Slf4j
@Configuration
public class InitRole implements ApplicationRunner {

    @Autowired
    RequestMappingHandlerMapping rmhm;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("初始化角色权限");
        Set<MethodRole> methodRoles = initMethodRole();
        for (MethodRole methodRole : methodRoles) {
            log.info("接口:{}", JSONUtil.toJsonStr(methodRole));
        }
    }


    public Set<MethodRole> initMethodRole() {
        Set<MethodRole> methodRoles = new HashSet<>();
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Set<Map.Entry<RequestMappingInfo, HandlerMethod>> entries = handlerMethods.entrySet();
        for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : entries) {
            HandlerMethod handlerMethod = entry.getValue();
            PreRole preRole = handlerMethod.getMethodAnnotation(PreRole.class);
            if (preRole != null) {
                RequestMappingInfo key = entry.getKey();
                PatternsRequestCondition patternsCondition = key.getPatternsCondition();
                Set<String> patterns = patternsCondition.getPatterns();

                Set<String> roles = new HashSet<>();
                String name = preRole.name();
                String role = preRole.role();
                if (StrUtil.isNotBlank(role)) {
                    roles.add(role);
                } else {
                    String[] rolesStr = preRole.roles();
                    if (rolesStr != null && rolesStr.length > 0) {
                        for (String s : rolesStr) {
                            roles.add(s);
                        }
                    }
                }

                for (String pattern : patterns) {
                    for (String roleStr : roles) {
                        MethodRole methodRole = new MethodRole();

                        methodRole.setUrl(pattern);
                        methodRole.setRole(roleStr);
                        methodRole.setName(name);
                        methodRoles.add(methodRole);
                    }
                }
            }
        }
        return methodRoles;
    }
}
